package com.zzz.gp.service.analyse;

import com.zzz.gp.dao.GpMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @description:上升比例
 * @author: ZZZ
 */
@Slf4j
@Service
public class RiseProportionService {

    @Autowired
    private GpMapper gpMapper;

    @Async("myAsync")
    public void todayRiseProportionSum() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today = dtf.format(LocalDateTime.now());
        LocalDateTime s = LocalDateTime.now();
        gpMapper.cleanRiseProportionInsert();
        gpMapper.riseProportionInsert();

        //近 5 10 20 30 60 90 120 150 天

        List<String> dayList = gpMapper.queryDayList();
        int sz5 = 0;
        int sz10 = 0;
        int sz20 = 0;
        int sz30 = 0;
        int sz60 = 0;
        int sz90 = 0;
        int sz120 = 0;
        int sz150 = 0;

        int n = dayList.size();
        if (n > 5) {
            sz5 = gpMapper.count5(dayList.get(0), dayList.get(4));
        }
        if (n > 10) {
            sz10 = gpMapper.count5(dayList.get(0), dayList.get(9));
        }
        if (n > 20) {
            sz20 = gpMapper.count5(dayList.get(0), dayList.get(19));
        }
        if (n > 30) {
            sz30 = gpMapper.count5(dayList.get(0), dayList.get(29));
        }
        if (n > 60) {
            sz60 = gpMapper.count5(dayList.get(0), dayList.get(59));
        }
        if (n > 90) {
            sz90 = gpMapper.count5(dayList.get(0), dayList.get(89));
        }
        if (n > 120) {
            sz120 = gpMapper.count5(dayList.get(0), dayList.get(119));
        }
        if (n > 150) {
            sz150 = gpMapper.count5(dayList.get(0), dayList.get(149));
        }
        log.info("========近 5 10 20 30 60 90 120 180 天涨情况=========");
        gpMapper.szRiseProportionInsert(sz5, today, "5");
        gpMapper.szRiseProportionInsert(sz10, today, "10");
        gpMapper.szRiseProportionInsert(sz20, today, "20");
        gpMapper.szRiseProportionInsert(sz30, today, "30");
        gpMapper.szRiseProportionInsert(sz60, today, "60");
        gpMapper.szRiseProportionInsert(sz90, today, "90");
        gpMapper.szRiseProportionInsert(sz120, today, "120");
        gpMapper.szRiseProportionInsert(sz150, today, "150");


        int k = 4;
        for (String nowDay : dayList) {
            if (k >= dayList.size()) {
                break;
            }
            String pastDay = dayList.get(k);
            int count = gpMapper.count5(nowDay, pastDay);
            gpMapper.szRiseProportionInsert(count, nowDay, "555");
            log.info(String.format("========过去近5天涨情况  %s  %d=========", nowDay, count));
            k = k + 1;
        }

        k = 9;
        for (String nowDay : dayList) {
            if (k >= dayList.size()) {
                break;
            }
            String pastDay = dayList.get(k);
            int count = gpMapper.count5(nowDay, pastDay);
            gpMapper.szRiseProportionInsert(count, nowDay, "556");
            log.info(String.format("========过去近10天涨情况  %s  %d=========", nowDay, count));
            k = k + 1;
        }


        LocalDateTime e = LocalDateTime.now();
        long seconds = Duration.between(s, e).getSeconds();
        log.info("当天上涨比例统计 用时{}秒", seconds);
    }

}
